package org.vison.wonfu.framework.soa.spi;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.vison.wonfu.framework.soa.annotation.SoaMethod;
import org.vison.wonfu.framework.soa.annotation.SoaRef;
import org.vison.wonfu.framework.soa.governance.MetaData;
import org.vison.wonfu.framework.soa.governance.SoaConsumerMeta;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Slf4j
public class SoaRefAnnotationPostProcessor implements BeanPostProcessor, ApplicationContextAware {


    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> clazz = bean.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(SoaRef.class)) {
                try {
                    field.setAccessible(true);
                    Object proxy = SoaRefProxyFactory.createProxy(field,applicationContext);
                    collectConsumerMeta(field,proxy);
                    //在运行时动态修改对象的字段值,将被注解的字段替换为代理对象
                    field.set(bean, proxy);
                } catch (IllegalAccessException e) {
                    log.error("SoaRefProxyFactory error",e);
                    throw new RuntimeException("SoaRefProxyFactory error");
                }
            }
        }
        return bean;
    }

    // 收集 consumer meta 信息
    public void collectConsumerMeta(Field  field,Object proxyObject) {
        SoaRef soaRef =  field.getAnnotation(SoaRef.class);
        String toAppid = soaRef.value();
        Class<?> clazz = field.getType();
        // 获取该接口的所有方法
        Method[] methods =  clazz.getDeclaredMethods();
        String servicePath = soaRef.path();
        for (Method method : methods) {
            String name = method.getName();
            SoaMethod soaMethod = method.getAnnotation(SoaMethod.class);
            // 没有标注 SoaMethod  则为标准SOA调用
            String consumerKey = MetaData.buildConsumerKey(toAppid,servicePath,name);
            SoaConsumerMeta consumerMeta;
            if (soaMethod != null) {
                String methodName = soaMethod.methodName();
                 consumerMeta =  SoaConsumerMeta.builder()
                        .toAppid(toAppid)
                        .commandKey(consumerKey)
                        .consumerClass(clazz.getName())
                        .servicePath(servicePath)
                        .method(name)
                        .methodDesc(methodName)
                        .build();
            }else{
                 consumerMeta =  SoaConsumerMeta.builder()
                        .toAppid(toAppid)
                        .commandKey(consumerKey)
                        .consumerClass(clazz.getName())
                        .servicePath(servicePath)
                        .method(name)
                        .build();
            }
            MetaData.addConsumerMeta(consumerKey,consumerMeta);
        }

    }
}